package com.study.d12_tcp_mul3;

import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws Exception {
        //目标：完成TCP通信入门，实现一发一收

        //创建Socket对象，同时请求与服务端的连接
        Socket socket = new Socket("127.0.0.1", 8888);

        //创建一个独立的线程，负责随时从socket中接受服务端发过来的消息
        new ClientReaderThread(socket).start();

        //从Socket通信管道中得到一个字节输出流，用来发数据给服务端程序
        OutputStream outputStream = socket.getOutputStream();

        //把低级的字节输出流包装成数据输出流
        DataOutputStream dos = new DataOutputStream(outputStream);
        Scanner sc = new Scanner(System.in);
        //开始写数据出去
        while (true) {
            System.out.println("输入消息，exit退出：");
            String msg = sc.nextLine();
            if("exit".equals(msg)){
                dos.close();
                socket.close();
                break;
            }
            dos.writeUTF(msg);
        }
    }
}
